package com.nzw.controller.common;

import com.aliyun.oss.common.utils.BinaryUtil;
import com.nzw.common.Constant;
import com.nzw.common.service.aliyun.AliyunOSS;
import com.nzw.common.service.uedit.ActionEnter;
import com.nzw.utils.MD5Utils;
import com.nzw.vo.common.HttpResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by AB350 on 2017/11/12.
 */
@RestController
public class BaseController extends AbstractController{
    /**
     * 公共的上传接口
     *
     * @param target     目标类型
     * @param uploadFile 上传的文件
     * @return
     */
    @PostMapping("/wx/upload/{target}")
    public HttpResult upload(@PathVariable String target, @RequestParam(value = "file") MultipartFile uploadFile) throws Exception {
    	
        HttpResult result = new HttpResult();
        switch (target) {
            case "img"://上传图片
                String md5 = MD5Utils.bytes2Md5Code(BinaryUtil.calculateMd5(uploadFile.getBytes()));
                String path = AliyunOSS.uploadFile(Constant.IMG_UPLOAD_TEMP_PATH, md5, uploadFile);
                result.setMsg(path);
                result.setObj(md5);
                result.setCode(HttpResult.OK);
                break;
                
        }
        return result;
    }

    /**
     * uEidt的配置
     *
     * @author zhansi.Xu
     * @DateTime 2016年9月30日
     * @serverComment
     */
    @GetMapping(value = "ueditConf")
    public void config(HttpServletRequest request,
                       HttpServletResponse response, String action) {
        response.setContentType("application/json");
        String rootPath = request.getSession().getServletContext().getRealPath("/");
        try {
            String exec = new ActionEnter(request, rootPath).exec();
            PrintWriter writer = response.getWriter();
            writer.write(exec);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
